﻿Imports System.Xml
Imports System.ComponentModel
Imports System.IO
Imports ajayumi.develop.VBasic.Core
Imports ajayumi.develop.VBasic.Core.Win
Imports ajayumi.develop.VBasic.Core.Win.WinAPI
Imports ajSerializer = ajayumi.develop.VBasic.Core.Xml.Serialization.Serializer
Imports ajayumi.develop.VBasic.Core.Drawing

Partial Public Class FrmMain
    Inherits ajayumi.develop.VBasic.Win.Controls.Forms.ImageForm

#Region "variable"

    Private Const SHOW_HIDE_ID As Integer = 7003 '显示/隐藏的系统键值
    Private Const EXIT_ID As Integer = 7004      '退出程序的系统键值

    Private m_Trochoid As ITrochoid = Nothing

#End Region

    Protected Overrides Function InitFormImage() As System.Drawing.Image
        Dim _Image As Image = Nothing
        Dim _OrigianImage As Image = Nothing

        Using _ImageHelper As New ImageHelper()
            If String.IsNullOrEmpty(My.Settings.MainPicture) = False AndAlso File.Exists(My.Settings.MainPicture) Then
                _OrigianImage = Image.FromFile(My.Settings.MainPicture)
            Else
                _OrigianImage = My.Resources.main
            End If

            _Image = _ImageHelper.GetThumbnailImage(_OrigianImage, 300, 300)
        End Using

        Return _Image
    End Function

    Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
        MyBase.OnLoad(e)
        Me.initializeComponent()

        Me.Tip = "快速启动程序"
        Me.ShowTip = True
        Me.TipHorizontalAlign = VBasic.Win.Controls.Forms.TipHorizontalAlign.Center
        Me.TipVerticalAlign = VBasic.Win.Controls.Forms.TipVerticalAlign.Center

        '注册热键
        'User32Dll.RegisterHotKey(Handle, SHOW_HIDE_ID, _
        '                         KeyModifiers.Control, Keys.A)
        'User32Dll.RegisterHotKey(Handle, EXIT_ID, _
        '                         KeyModifiers.Control, Keys.S)

        User32Dll.RegisterHotKey(Handle, SHOW_HIDE_ID, _
                                 CType(My.Settings.Show_Hide_KeyModifiers, KeyModifiers), My.Settings.Show_Hide_Key)
        User32Dll.RegisterHotKey(Handle, EXIT_ID, _
                                 CType(My.Settings.Exit_KeyModifiers, KeyModifiers), My.Settings.Exit_Key)


        Me.m_Trochoid = New LineTrochoid(Me.Bounds) With {.ItemSize = New System.Drawing.Size(32, 32)}

        Me.m_BackgroundWorker.RunWorkerAsync()
    End Sub

    Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
        '注销热键
        User32Dll.UnregisterHotKey(Handle, SHOW_HIDE_ID)
        User32Dll.UnregisterHotKey(Handle, EXIT_ID)

        MyBase.OnClosing(e)
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case &H312  '这个是window消息定义的   注册的热键消息 
                If m.WParam.ToString().Equals(CStr(SHOW_HIDE_ID)) Then
                    If Me.Visible Then
                        Me.FormHide()
                    Else
                        Me.FormShow()
                    End If
                ElseIf m.WParam.ToString().Equals(CStr(EXIT_ID)) Then
                    Me.Close()
                End If
            Case Else
                Exit Select
        End Select

        MyBase.WndProc(m)
    End Sub

    Protected Overrides Sub MouseMoveAttachWork(ByVal offsetX As Integer, ByVal offsetY As Integer)
        Me.m_Trochoid.Move(offsetX, offsetY)
    End Sub

    Protected Overrides Sub Form_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then
            Me.FormHide()
        End If
    End Sub

    Protected Overrides Sub OnVisibleChanged(ByVal e As System.EventArgs)
        MyBase.OnVisibleChanged(e)
        If Me.Visible Then
            Me.m_miShowOrHide.Text = "隐藏"
        Else
            Me.m_miShowOrHide.Text = "显示"
        End If
    End Sub

    ''' <summary>
    ''' 从配置文件中读取应用程序列表
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function ReadConfig() As List(Of ProgramInfo)
        Dim _List As New List(Of ProgramInfo)()
        Dim _FileContents As Byte()
        If File.Exists(o_ConfigPath) Then
            _FileContents = My.Computer.FileSystem.ReadAllBytes(o_ConfigPath)
            _List = ajSerializer.DeSerializeFromXml(Of List(Of ProgramInfo))(_FileContents)
            For Each _ProgramInfo As ProgramInfo In _List
                If String.IsNullOrEmpty(_ProgramInfo.FormImageData) = False Then
                    _ProgramInfo.FormImage = ImageHelper.Base64StringToImage(_ProgramInfo.FormImageData)
                Else
                    _ProgramInfo.FormImage = My.Resources.none
                End If
            Next
        End If
        '_List.Add(New ProgramInfo() With {.Id = 1, .Name = "a", .FormImage = My.Resources.none})
        '_List.Add(New ProgramInfo() With {.Id = 2, .Name = "b", .FormImage = My.Resources.none})
        '_List.Add(New ProgramInfo() With {.Id = 3, .Name = "c", .FormImage = My.Resources.none})
        '_List.Add(New ProgramInfo() With {.Id = 4, .Name = "d", .FormImage = My.Resources.none})
        '_List.Add(New ProgramInfo() With {.Id = 5, .Name = "e", .FormImage = My.Resources.none})
        '_List.Add(New ProgramInfo() With {.Id = 6, .Name = "f", .FormImage = My.Resources.none})

        Return _List
    End Function


    Private Sub BindData()
        Dim _List As List(Of ProgramInfo) = Me.ReadConfig()
        Me.m_Trochoid.Clear()
        For Each _ProgramInfo As ProgramInfo In _List
            _ProgramInfo.FormImageSize = Me.m_Trochoid.ItemSize
            Dim _ImageForm As New ImageForm(_ProgramInfo)
            _ImageForm.Owner = Me
            Me.m_Trochoid.Add(_ImageForm)
        Next
    End Sub

    Private Sub FormShow()
        Me.MoveToCenterScreen()
        Me.Show()
        Me.m_BackgroundWorker.RunWorkerAsync()
    End Sub

    Private Sub FormHide()
        Me.Hide()
        Me.m_Trochoid.Hide()
    End Sub


End Class
